<template>
  <div class='edit-name'>
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('closeEditNameShow')"
      @click-right="updateUserName"
    />
    <div class="nameInputWrap">
      <van-field
        class="name-input"
        v-model="localName"
        placeholder="请输入昵称"
        maxlength="7"
        round
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import { Toast } from 'vant'

export default {
  name: 'editName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      userName: '',
      localName: this.value
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async updateUserName () {
      try {
        Toast.loading({
          message: '加载中...',
          forbidClick: true
        })
        await updateUserProfile({
          name: this.localName
        })
        this.$emit('closeEditNameShow')
        this.$emit('input', this.localName)
        Toast.success('更改成功')
      } catch (error) {
        console.dir(error)
        if (error && error.response && error.response.status === 401) {
          Toast.fail('请重新登录')
        }
      }
    }
  }
}
</script>
<style lang='less' scoped>
.nameInputWrap {
  padding: 20px 14px;
  background-color: rgb(248, 248, 248);
  .van-cell{
    padding: 10px 6px;
  }
  /deep/ .van-field__control {
    text-indent: 0px;
    font-size: 16px;
  }
  .name-input {
    border-radius: 4px;
    text-indent: 20px;
  }
}
</style>
